<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <script>
    // 百度：https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=h&cb=show
    //简单封装jsonp
    function jsonp({
      url,
      params,
      cb
    }) {
      return new Promise((resolve, reject) => {
        let script = document.createElement('script');
        window[cb] = function (data) {
          resolve(data)
          window.document.head.removeChild(script);
        }
        params = {
          ...params,
          cb
        } //wd=h&cb=show
        let arr = []
        for (let key in params) {
          arr.push(`${key}=${params[key]}`)
        }

        script.src = `${url}?${arr.join('&')}`
        console.log(script.src);
        window.document.head.appendChild(script);
      })
    }
    //缺点只能get，可以xss攻击
    //调用
    jsonp({
      url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
      params: {
        wd: 'h'
      },
      cb: 'show'
    }).then(data => {
      console.log(data);

    })
  </script>
  <!-- <script src="https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=h&cb=show"></script> -->

</body>

</html>